package com.jiepu.daily.corejava.advanceed.part6thread;

import org.jetbrains.annotations.NotNull;

/**
 * @Description: 龟兔赛跑 100m
 * @author: xinjie.txt
 * @date: 2020/7/29  16:58
 */
public class TortoiseAndRabbitRace1 {

	// Rabbit OR Tortoise
	static class Animal extends Thread {
		long time;

		public Animal(@NotNull String name) {
			super(name);
		}

		@Override
		public void run() {
			int sum = 0;
			long t1 = System.currentTimeMillis();
			while (sum < 100) {
				// 1 ~ 10
				int meter = (int) (Math.random() * 10 + 1);
				sum += meter;

				try {
					sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				System.out.println(getName() + " This turn: " + meter
						+ " Totol: " + sum);

			} // while
			long t2 = System.currentTimeMillis();
			time = t2-t1;

		}
	} // Animal Thread


	public static void main(String[] args) {
		Animal a1 = new Animal("Rabbit");
		Animal a2 = new Animal("Tortoise");

		a1.start();
		a2.start();

		System.out.println("12333333---------");
		System.out.println(a1.getName()+": "+a1.time);
		System.out.println(a2.getName()+": "+a2.time);
	}
}
